package model;

import security.Irreversible;
import security.MD5Hex;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Objects;

public class User {

    private Long          u_id;
    private String        u_name;
    private String        u_passwd;
    private String        u_nick;
    private String        u_tel;
    private String        u_state;
    private LocalDateTime u_vip_ddate;
    private Long          u_score;
    private BigDecimal    u_bank;
    private String        u_pic_address;
    private String        u_recv_address;

    public User(Long u_id, String u_name, String u_passwd, String u_nick, String u_tel, String u_state, LocalDateTime u_vip_ddate, Long u_score, BigDecimal u_bank, String u_pic_address, String u_recv_address) {
        this.u_id           = u_id;
        this.u_name         = u_name;
        this.u_passwd       = u_passwd;
        this.u_nick         = u_nick;
        this.u_tel          = u_tel;
        this.u_state        = u_state;
        this.u_vip_ddate    = u_vip_ddate;
        this.u_score        = u_score;
        this.u_bank         = u_bank;
        this.u_pic_address  = u_pic_address;
        this.u_recv_address = u_recv_address;
    }

    public User(Object[] recordRow) {
        this.u_id           = (Long)recordRow[0];
        this.u_name         = (String)recordRow[1];
        this.u_passwd       = (String)recordRow[2];
        this.u_nick         = (String)recordRow[3];
        this.u_tel          = (String)recordRow[4];
        this.u_state        = (String)recordRow[5];
        this.u_vip_ddate    = (LocalDateTime)recordRow[6];
        this.u_score        = (Long)recordRow[7];
        this.u_bank         = (BigDecimal) recordRow[8];
        this.u_pic_address  = (String)recordRow[9];
        this.u_recv_address = (String)recordRow[10];
    }

    public User(ResultSet rs) throws SQLException {
        this.u_id           = rs.getLong("u_id");
        this.u_name         = rs.getString("u_name");
        this.u_passwd       = rs.getString("u_passwd");
        this.u_nick         = rs.getString("u_nick");
        this.u_tel          = rs.getString("u_tel");
        this.u_state        = rs.getString("u_state");
        this.u_vip_ddate    = rs.getTimestamp("u_vip_ddate").toLocalDateTime();
        this.u_score        = rs.getLong("u_score");
        this.u_bank         = rs.getBigDecimal("u_bank");
        this.u_pic_address  = rs.getString("u_pic_address");
        this.u_recv_address = rs.getString("u_recv_address");
    }

    @Override
    public String toString() {
        return "User{" +
                "u_id=" + u_id +
                ", u_name='" + u_name + '\'' +
                ", u_passwd='" + u_passwd + '\'' +
                ", u_nick='" + u_nick + '\'' +
                ", u_tel='" + u_tel + '\'' +
                ", u_state='" + u_state + '\'' +
                ", u_vip_ddate=" + u_vip_ddate +
                ", u_score=" + u_score +
                ", u_bank=" + u_bank +
                ", u_pic_address='" + u_pic_address + '\'' +
                ", u_recv_address='" + u_recv_address + '\'' +
                '}';
    }

    public User(){}

    public User(long id)
    {setU_id(id);}

    //使用的加密方式
    //private final static Irreversible<String, String> passwordMaker = new MD5Hex();

    public String getU_recv_address() {
        return u_recv_address;
    }

    public void setU_recv_address(String u_recv_address) {
        this.u_recv_address = u_recv_address;
    }

    public Long getU_id() {
        return u_id;
    }

    public void setU_id(Long u_id) {
        this.u_id = u_id;
    }

    public String getU_name() {
        return u_name;
    }

    public void setU_name(String u_name) { this.u_name = u_name; }

    public String getU_passwd() {
        return u_passwd;
    }

    public void setU_passwd(String u_passwd) { this.u_passwd = u_passwd; }

    public String getU_nick() {
        return u_nick;
    }

    public void setU_nick(String u_nick) {
        this.u_nick = u_nick;
    }

    public String getU_tel() {
        return u_tel;
    }

    public void setU_tel(String u_tel) {
        this.u_tel = u_tel;
    }

    public String getU_state() {
        return u_state;
    }

    public void setU_state(String u_state) {
        this.u_state = u_state;
    }

    public LocalDateTime getU_vip_ddate() {
        return u_vip_ddate;
    }

    public void setU_vip_ddate(LocalDateTime u_vip_ddate) {
        this.u_vip_ddate = u_vip_ddate;
    }

    public Long getU_score() {
        return u_score;
    }

    public void setU_score(Long u_score) {
        this.u_score = u_score;
    }

    public BigDecimal getU_bank() {
        return u_bank;
    }

    public void setU_bank(BigDecimal u_bank) {
        this.u_bank = u_bank;
    }

    public String getU_pic_address() {
        return u_pic_address;
    }

    public void setU_pic_address(String u_pic_address) {
        this.u_pic_address = u_pic_address;
    }

    public boolean isEmpty()
    {
        return
            u_id == null  && u_name == null && u_passwd == null &&
            u_nick == null && u_tel == null && u_state == null &&
            u_vip_ddate == null && u_score == null && u_bank == null &&
            u_pic_address == null && u_recv_address == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return u_id.equals(user.u_id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(u_id);
    }
}
